package com.boroborome.convert;

import java.time.Instant;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AppCallRecord implements ICallRecord {
    private Instant logTime;
    private String methodKey;
    private double time;

    @Override
    public Instant getLogTime() {
        return logTime;
    }

    @Override
    public String getFunctionId() {
        return methodKey;
    }

    public double getTime() {
        return time;
    }

    private static Pattern logPattern = Pattern.compile("Remote Call Method (.*) (success|failed) in (\\d+) ms(.*)");
    public static AppCallRecord parse(String[] items) {
        AppCallRecord record = new AppCallRecord();
        Matcher result = logPattern.matcher(items[1]);
        if (result.find()) {
            Instant instant = Instant.parse(items[0]);
            record.logTime = instant;
            record.methodKey = result.group(1);
            record.time = Double.parseDouble(result.group(3));
        } else {
            System.out.println(items[1]);
        }

        return record;
    }


    private static String parseUrl(String item) {
        int questionMaskIndex = item.indexOf('?');

        return questionMaskIndex > 0
                ? item.substring(0, questionMaskIndex)
                : item;
    }

    private static double readTime(String item) {
        return Double.parseDouble(item);
    }

}
